//
//  ScrollWithImage.m
//  The Station Agent
//
//  Created by dllo on 16/1/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ScrollWithImage.h"
#import <YYWebImage.h>
@interface ScrollWithImage ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) UIPageControl *page;
@property (nonatomic, retain) NSMutableArray *arrImages;
@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, retain) NSMutableArray *arrUrl;

@end
@implementation ScrollWithImage
- (void)dealloc {
   
}
- (instancetype)initWithFrame:(CGRect)frame images:(NSArray<NSString *> *)images url:(NSArray<NSString *> *)url withTimer:(NSTimeInterval)timeinterval {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.arrImages = [NSMutableArray arrayWithArray:images];
        [self.arrImages addObject:self.arrImages.firstObject];
        [self.arrImages insertObject:[self.arrImages objectAtIndex:self.arrImages.count - 2] atIndex:0];
        self.backgroundColor = [UIColor yellowColor];
        
        if (url) {
            self.arrUrl = [NSMutableArray arrayWithArray:url];
        }
        [self.arrUrl addObject:self.arrUrl.firstObject];
        [self.arrUrl insertObject:[self.arrUrl objectAtIndex:self.arrUrl.count - 2] atIndex:0];
        
        // 创建计时器
        if (!self.timer) {
            self.timer = [NSTimer scheduledTimerWithTimeInterval:timeinterval target:self selector:@selector(scrollRight) userInfo:nil repeats:YES];
            
        }
        [self createScrollerWithFrame:frame];
        
        [self createPageControl];
        
      
    }
    return self;
    
}

- (void)createScrollerWithFrame:(CGRect)frame {
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [self addSubview:self.scroll];
    
    self.scroll.showsHorizontalScrollIndicator = NO;
    
    self.scroll.contentSize = CGSizeMake(self.arrImages.count *frame.size.width , frame.size.height);
    self.scroll.delegate = self;
    
    
    self.scroll.pagingEnabled = YES;
    self.scroll.backgroundColor = [UIColor lightGrayColor];
    
    [self createImageViewWithFrame:frame];
    
}
- (void)createImageViewWithFrame:(CGRect)frame {
    
    for (int i = 0; i < self.arrImages.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height)];
        imageView.tag = i;
        
        imageView.yy_imageURL = [NSURL URLWithString:self.arrImages[i]];
        
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;//超越父视图边界以外不显示。
        [self.scroll addSubview:imageView];
        imageView.userInteractionEnabled = YES;
        
//        // 给imageView添加轻拍手势
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapAction:)];
//        
//        [imageView addGestureRecognizer:tap];
//        
//        [tap release];
    }
     // 偏移量
    self.scroll.contentOffset = CGPointMake(self.frame.size.width, 0);


}
//- (void)handleTapAction:(UIGestureRecognizer *)tap {
//    NSInteger i = [tap view].tag;
//    NSString *url = [self.arrUrl objectAtIndex:i];
//
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"clickScroll" object:nil userInfo:@{@"clickScroll":url}];
//    
//}
#pragma mark - Scroll Delegate
// 实现循环播放
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.x == 0) {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width *(self.arrImages.count - 2), 0)];
    }
    
    if (scrollView.contentOffset.x == self.frame.size.width * (self.arrImages.count - 1)) {
        
        [scrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
    }
    
    if (scrollView.contentOffset.x == self.frame.size.width) {
        self.page.currentPage = 0;
    } if (scrollView.contentOffset.x == self.frame.size.width * 2) {
        self.page.currentPage = 1;
    } if (scrollView.contentOffset.x == self.frame.size.width * 3) {
        self.page.currentPage = 2;
    } if (scrollView.contentOffset.x == self.frame.size.width * 4) {
        self.page.currentPage = 3;
    } if (scrollView.contentOffset.x == self.frame.size.width * 5) {
        self.page.currentPage = 4;
    } if (scrollView.contentOffset.x == self.frame.size.width * 6) {
        self.page.currentPage = 5;
    } if (scrollView.contentOffset.x == self.frame.size.width * 7) {
        self.page.currentPage = 6;

    }
    
}

#pragma mark - page
- (void)createPageControl {
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width / 10 * 3,self.frame.size.height - 25, self.frame.size.width, 30)];
    [self addSubview:self.page];
    
    self.page.numberOfPages = self.arrImages.count - 2;
    
    [self.page addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    
}
- (void)pageChange:(UIPageControl *)page {
    
    [self.scroll setContentOffset:CGPointMake(self.frame.size.width *self.page.currentPage, 0) animated:YES];
    
    
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.timer invalidate];
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(scrollRight) userInfo:nil repeats:YES];
    
}
#pragma mark - timer
- (void)scrollRight {
 
    [UIView animateWithDuration:0.5 animations:^{
        
        self.scroll.contentOffset = CGPointMake(self.scroll.contentOffset.x + SCREEN_WIDTH , 0);
        
    } completion:^(BOOL finished) {
        
        [self scrollViewDidEndDecelerating:self.scroll];
        
        finished = YES;
        
    }];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
